<template>
  <el-dialog
    :title="btntype == 0 ? '添加' : '编辑'"
    :visible.sync="dialogVisible"
    width="50%"
    :before-close="handleClose"
  >
    <!--弹框 内容 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="所属角色" prop="role">
        <el-select v-model.number="ruleForm.role" placeholder="请选择所属角色">
          <el-option label="管理员" :value="0"></el-option>
          <el-option label="街道负责人" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="说明" prop="explain">
        <el-select v-model.number="ruleForm.explain" placeholder="请选择权限">
          <el-option label="管理所有权限" :value="0"></el-option>
          <el-option label="上传车位信息" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否有效" prop="whether">
        <el-select v-model.number="ruleForm.whether" placeholder="请选择是否">
          <el-option label="否" :value="0"></el-option>
          <el-option label="是" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialogfun">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >提 交</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { addwxApi, updatewxApi } from "../../../api/wxapi/wxApi";
export default {
  props: ["row", "btntype"],
  data() {
    return {
      arr: {},
      ruleForm: {
        id: "",
        name: "",
        role: "", //0 管理员  1 街道负责人
        explain: "", //0 管理所有权限 1 上传车位信息
        whether: "", //0 否  1 是
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        role: [
          { required: true, message: "请选择所属角色", trigger: "change" },
        ],
        explain: [{ required: true, message: "请选择权限", trigger: "change" }],
        whether: [
          { required: true, message: "请选择“是”还是“否”", trigger: "change" },
        ],
      },
      dialogVisible: true, //控制弹框的显示和隐藏
    };
  },
  mounted() {
    // this.ruleForm = this.row //赋值  浅拷贝
    // this.ruleForm = {...this.row} //赋值
    this.ruleForm = JSON.parse(JSON.stringify(this.row)); //赋值
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.btntype == 0) {
            //添加逻辑
            // 添加第二步
            addwxApi(this.ruleForm).then((res) => {
              // console.log("res",res);
              if (res.code == 200) {
                this.$emit("parentinti");
                this.$emit("parentclose");
                console.log(this.arr.id);
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
              }
            });
          } else if (this.btntype == 1) {
            //编辑逻辑
            //编辑第二步
            updatewxApi(this.ruleForm).then((res) => {
              console.log("res", res);
              if (res.code == 200) {
                console.log("df");
                this.$emit("parentinti");
                this.$emit("parentclose");
                  this.$message({
                  message: "编辑成功",
                  type: "success",
                });
              }
            });
          }
          //   alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    closeDialogfun() {
      this.$emit("parentclose");
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.closeDialogfun();
        })
        .catch((_) => {});
    },
  },
};
</script>
<style>
</style>